package org.medusa.network.tcp;

import org.medusa.core.EventPump;
import org.medusa.log.ILog;
import org.medusa.packet.blob.LinePacket;

public abstract class TcpAsyncLineConnector extends BaseTcpAsyncConnector<LinePacket> {

	private LinePacket half_packet_recv = null;

	public TcpAsyncLineConnector(String name, ILog log, EventPump evp,
			int rcv_buf, int snd_buf, int idle_second) {
		super(name, log, evp, rcv_buf, snd_buf, idle_second);
	}

	@Override
	protected LinePacket parse_packet() {
		LinePacket line = null;
		if (this.half_packet_recv == null)
			line = new LinePacket(this.ringbuf.DataSize()
					+ (this.ringbuf.DataSize() / 2));
		else
			line = this.half_packet_recv;
		
		byte b;
		while (this.ringbuf.DataSize() > 0) {
			b = this.ringbuf.ReadInt8();
			line.WriteByte(b);
			if (b == '\n') {
				this.half_packet_recv = null;
				return line;
			}
		}

		return null;
	}

}
